﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using App.Data;
using System.Data;

public partial class Pages_Admin_CustomerUserList : BasePage
{
    private int _CustomerID = 0;
    private int _UserID = 0;
    private bool _IsDeleteMode = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            CheckAndDeleteData();
            //BindDropdownList.Customers(ddlCustomer, ConfigReader.DeveloperBusinessTypeID);
            BindDropdownList.BusinessReresentative(ddlCustomer);

            BindExistingList();
        }
    }
    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
        _UserID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
        _CustomerID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.CUSTOMER_ID);
        _IsDeleteMode = String.Compare(WebUtil.GetQueryStringInString(AppConstants.QueryString.DELETE), "True", false) == 0 ? true : false;        
    }
    protected void BindExistingList()
    {
        if (_CustomerID > 0)
        {
            ddlCustomer.SetSelectedItem(_CustomerID.ToString());
            BindUserList(1);
        }
    }
    protected void CheckAndDeleteData()
    {
        if (_IsDeleteMode)
        {
            BtcHousingEntities context = new BtcHousingEntities();
            User user = context.Users.FirstOrDefault(P => P.ID == _UserID && P.CustomerID == _CustomerID);
            if (user == null)
                WebUtil.ShowMessageBox(divMessage, "Sorry! Requested User was not found for delete.", true);
            else
            {
                user.IsDeleted = true;
                user.IsActive = false;
                context.SaveChanges();
                WebUtil.ShowMessageBox(divMessage, "User Deleted Successfully.", false);
            }
        }
    }
    protected void BindUserList(int pageNumber)
    {
        int customerID = ddlCustomer.SelectedValue.ToInt();
        int totalRecord = 0;
        UtilityDAO dao = new UtilityDAO();
        App.Data.DbParameter[] parameters = new[] { new App.Data.DbParameter("@CustomerID", customerID) };
        DataSet ds = dao.GetPagedData(AppSQL.GET_PAGED_USERS_BY_CUSTOMER, parameters, pageNumber, ConfigReader.CommonListPageSize, out totalRecord);
        ucUserList.DataSource = ds.Tables[0];
        ucUserList.EditLink = AppConstants.Pages.CUSTOMER_USER_CHANGE + "?" + AppConstants.QueryString.ID + "={0}&" + AppConstants.QueryString.CUSTOMER_ID + "={1}";
        ucUserList.DeleteLink = Request.Url.AbsolutePath + "?" + AppConstants.QueryString.ID + "={0}&" + AppConstants.QueryString.CUSTOMER_ID + "={1}&" + AppConstants.QueryString.DELETE + "=True";
        ucUserList.LinkFields = "ID, CustomerID";
        ucUserList.DataBind();

        divListingContainer.Visible = true;
        hplAddUserLink.NavigateUrl = String.Format("{0}?{1}={2}", AppConstants.Pages.CUSTOMER_USER_CHANGE, AppConstants.QueryString.CUSTOMER_ID, ddlCustomer.SelectedValue);

        ///Bind Pager
        ucUserListPager.TotalRecord = totalRecord;
        ucUserListPager.PageNo = pageNumber;
        ucUserListPager.PageSize = ConfigReader.CommonListPageSize;
        ucUserListPager.TotalRecordMessage = "Total {0} User(s) Found.";
        ucUserListPager.DataBind();

    }
    //protected void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
    //{
    //    BindUserList();
    //}
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            BindUserList(1);
        }
    }
    protected void ucUserListPager_PageIndexChanged(object sender, PagerEventArgs e)
    {
        BindUserList(e.PageIndex);
    }
}